package com.example.wzh.rxdemo;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.View;

import androidx.annotation.ColorRes;
import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * @author wangjun
 * @version 1.0
 * @date 2016/8/25
 */
@SuppressWarnings({"unused", "RedundantSuppression"})
public class RecyclerSpace extends RecyclerView.ItemDecoration {
    public static final int STYLE_NO_EDGE = 0;
    public static final int STYLE_WITH_EDGE = 1;

    @Retention(RetentionPolicy.SOURCE)
    @IntDef({STYLE_NO_EDGE, STYLE_WITH_EDGE})
    @interface Style {
    }
    @SuppressWarnings("FieldMayBeFinal")
    private int space;
    private int color = -1;
    private Drawable mDivider;
    private Paint mPaint;
    private @Style
    int style;

    public int getColor() {
        return color;
    }

    public void setColor(@ColorRes int color) {
        this.color = color;
    }

    public RecyclerSpace(int space) {
        this.space = space;
    }

    public RecyclerSpace(int space, int color) {
        this.space = space;
        this.color = color;
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(color);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(space * 2);
    }

    public RecyclerSpace(int space, int color, @Style int style) {
        this.space = space;
        this.color = color;
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(color);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(space * 2);
        this.style = style;
    }

    public RecyclerSpace(int space, Drawable mDivider) {
        this.space = space;
        this.mDivider = mDivider;
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view,
                               RecyclerView parent, @NonNull RecyclerView.State state) {
        if (parent.getLayoutManager() != null) {
            if (parent.getLayoutManager() instanceof LinearLayoutManager && !(parent.getLayoutManager() instanceof GridLayoutManager)) {
                if (((LinearLayoutManager) parent.getLayoutManager()).getOrientation() == LinearLayoutManager.HORIZONTAL) {
                    outRect.set(space, 0, space, 0);
                } else {
                    outRect.set(0, space, 0, space);
                }
            } else {
                outRect.set(space, space, space, space);
            }
        }

    }

    @Override
    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDraw(c, parent, state);
        if (parent.getLayoutManager() != null) {
            if (parent.getLayoutManager() instanceof LinearLayoutManager && !(parent.getLayoutManager() instanceof GridLayoutManager)) {
                if (((LinearLayoutManager) parent.getLayoutManager()).getOrientation() == LinearLayoutManager.HORIZONTAL) {
                    drawHorizontal(c, parent);
                } else {
                    drawVertical(c, parent);
                }
            } else {
                if (style == STYLE_NO_EDGE) {
                    drawGridView(c, parent);
                } else {
                    drawGridView1(c, parent);
                }
            }
        }
    }

    //绘制纵向 item 分割线

    private void drawVertical(Canvas canvas, RecyclerView parent) {
        final int top = parent.getPaddingTop();
        final int bottom = parent.getMeasuredHeight() - parent.getPaddingBottom();
        final int childSize = parent.getChildCount();
        for (int i = 0; i < childSize; i++) {
            final View child = parent.getChildAt(i);
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int left = child.getRight() + layoutParams.rightMargin;
            final int right = left + space;
            if (mDivider != null) {
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(canvas);
            }
            if (mPaint != null) {
                canvas.drawRect(left, top, right, bottom, mPaint);
            }
        }
    }

    //绘制横向 item 分割线
    private void drawHorizontal(Canvas canvas, RecyclerView parent) {
        int left = parent.getPaddingLeft();
        int right = parent.getMeasuredWidth() - parent.getPaddingRight();
        final int childSize = parent.getChildCount();
        for (int i = 0; i < childSize; i++) {
            final View child = parent.getChildAt(i);
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
            int top = child.getBottom() + layoutParams.bottomMargin;
            int bottom = top + space;
            if (mDivider != null) {
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(canvas);
            }
            if (mPaint != null) {
                canvas.drawRect(left, top, right, bottom, mPaint);
            }

        }
    }

    //绘制gridView item 分割线 不是填充满的
    private void drawGridView(Canvas canvas, RecyclerView parent) {
        GridLayoutManager linearLayoutManager = (GridLayoutManager) parent.getLayoutManager();
        int childSize = parent.getChildCount();
        assert linearLayoutManager != null;
        int spanCount = linearLayoutManager.getSpanCount();
        for (int i = 0; i < childSize; i++) {
            final View child = parent.getChildAt(i);
            boolean isLastColumn = (i + 1) % spanCount == 0;
            int completeRowCount = childSize / spanCount;
            boolean isLastRow = i > spanCount * completeRowCount - 1;
            if (!isLastRow) {
                drawBottomLine(canvas, child);
            }
            if (!isLastColumn) {
                drawRightLine(canvas, child);
            }
        }
    }

    /***/
    private void drawGridView1(Canvas canvas, RecyclerView parent) {
        GridLayoutManager linearLayoutManager = (GridLayoutManager) parent.getLayoutManager();
        int childSize = parent.getChildCount();
        assert linearLayoutManager != null;
        int spanCount = linearLayoutManager.getSpanCount();
        for (int i = 0; i < childSize; i++) {
            final View child = parent.getChildAt(i);
            //画横线
            drawBottomLine(canvas, child);
            //画竖线
            drawRightLine(canvas, child);
            //画上缺失的线框
            if (i < spanCount) {
                drawTopLine(canvas, child);
            }
            if (i % spanCount == 0) {
                drawLeftLine(canvas, child);
            }
        }
    }

    private void drawBottomLine(Canvas canvas, View child) {
        int top, bottom, left, right;
        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
        top = child.getBottom() + layoutParams.bottomMargin;
        bottom = top + space;
        left = child.getLeft() - layoutParams.leftMargin - space;
        right = child.getRight() + layoutParams.rightMargin + space;
        if (mDivider != null) {
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(canvas);
        }
        if (mPaint != null) {
            canvas.drawRect(left, top, right, bottom, mPaint);
        }
    }

    private void drawRightLine(Canvas canvas, View child) {
        int top, bottom, left, right;
        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
        top = child.getTop() - layoutParams.topMargin - space;
        bottom = child.getBottom() + layoutParams.bottomMargin + space;
        left = child.getRight() + layoutParams.rightMargin;
        right = left + space;
        if (mDivider != null) {
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(canvas);
        }
        if (mPaint != null) {
            canvas.drawRect(left, top, right, bottom, mPaint);
        }
    }

    private void drawTopLine(Canvas canvas, View child) {
        int top, bottom, left, right;
        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
        top = child.getTop() - layoutParams.topMargin - space;
        bottom = child.getTop() - layoutParams.topMargin;
        left = child.getLeft() - layoutParams.leftMargin - space;
        right = child.getRight() + layoutParams.rightMargin + space;
        if (mDivider != null) {
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(canvas);
        }
        if (mPaint != null) {
            canvas.drawRect(left, top, right, bottom, mPaint);
        }
    }

    private void drawLeftLine(Canvas canvas, View child) {
        int top, bottom, left, right;
        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
        top = child.getTop() - layoutParams.topMargin - space;
        bottom = child.getBottom() + layoutParams.bottomMargin + space;
        left = child.getLeft() - layoutParams.leftMargin - space;
        right = child.getLeft() - layoutParams.leftMargin;
        if (mDivider != null) {
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(canvas);
        }
        if (mPaint != null) {
            canvas.drawRect(left, top, right, bottom, mPaint);
        }
    }
}

